﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SysMisc;
using PluginEngine;

namespace PrintingManufactureManageStation
{
    public class MenuStripEx:MenuStrip
    {
        public MenuStripEx()
            : base()
        {
            //this.MenuBar = true;
            this.Dock = DockStyle.Top;
            this.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

            string[] items_text = { "系统","雇员", "客户","供应商", "物料", "产品", "仓库","采购", "生产", "销售","财务", "物流", "报价" };
            string[] items_name = { "System", "雇员", "客户", "供应商", "物料", "产品", "仓库", "采购", "生产", "销售", "财务", "物流", "报价" };
            string[] items_tag = { "Type.System","Type.Employee", "Type.Customer", "Type.Supplier", "Type.Mate", "Type.Product","Type.Stock", "Type.Purchase", "Type.Manufacturing", "Type.Sale", "Type.Finance", "Type.Logistics", "Type.Quote"};
            int n = items_text.Length;
            for( int i=0;i<n;i++)
            {
                ToolStripMenuItem tsmi = new ToolStripMenuItem(items_text[i]);
                tsmi.Name = items_name[i];
                tsmi.Tag = items_tag[i];
                this.Items.Add(tsmi);
                //DevComponents.DotNetBar.ButtonItem bi = new DevComponents.DotNetBar.ButtonItem(items_text[i]);
                //bi.Name = items_name[i];
                //bi.Tag = items_tag[i];
                //this.Items.Add(bi);
            }

            ToolStripMenuItem tsmi_exit = new ToolStripMenuItem("注销");
            tsmi_exit.Tag = "Type.System";
            tsmi_exit.Click += MenuStripEvent_Logout;
            this.AddItem(tsmi_exit);
            tsmi_exit = new ToolStripMenuItem("退出");
            tsmi_exit.Tag = "Type.System";
            tsmi_exit.Click += MenuStripEvent_Exit;
            this.AddItem(tsmi_exit);
        }

        public IApplication App { set { _app = value; } }
        public AppConfig AppConfig { set { _appConfig = value; } }

        private IApplication _app;
        private AppConfig _appConfig;

        protected void MenuStripEvent_Logout(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认注销吗？！", "注销", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dr == DialogResult.Yes)
            {
                _app.UserLogined = false;
            }
        }

        protected  void MenuStripEvent_Exit( object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确认退出吗？！","退出",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
            if( dr == DialogResult.Yes)
            {
                (_app as Form).Close();
                //SysMisc.Common.ExitApplication();
            }
        }

        /// <summary>
        /// 虽然将来对于typ_name部分，你将来再看的时候，可能看不懂，但是相信我，这句绝对是没问题的，不要再尝试去读懂它的逻辑，那是浪费时间。
        /// </summary>
        /// <param name="tsmi"></param>
        public void AddItem( ToolStripMenuItem tsmi)
        {
            foreach( ToolStripMenuItem tsmi_father in this.Items)
            {
                string typ_name = null;
                if( typ_name == null )
                {
                    IAppModule app_mdl = tsmi.Tag as IAppModule;
                    if( null != app_mdl)
                    {
                        typ_name = app_mdl.Category;
                    }
                }
                if (typ_name == null)
                {
                    string name = tsmi.Tag as string;
                    if (null != name)
                    {
                        typ_name = name;
                    }
                }
                if (tsmi_father.Tag as string == typ_name )
                {
                    tsmi_father.DropDownItems.Add(tsmi);
                    return;
                }
            }
        }

        public void AddItem(IAppModule inf_app_module )
        {
            foreach (ToolStripMenuItem tsmi_father in this.Items)
            {
                if (tsmi_father.Tag as string == inf_app_module.Category )
                {
                    ToolStripMenuItem tsmi = new ToolStripMenuItem(inf_app_module.Caption);
                    tsmi_father.DropDownItems.Add(tsmi);
                    return;
                }
            }
        }

        /// <summary>
        /// 清空菜单栏
        /// </summary>
        public void Clear()
        {
            foreach (ToolStripMenuItem tsmi_father in this.Items)
            {
                if( false == (tsmi_father.Tag as string).Equals( "Type.System"))
                    tsmi_father.DropDownItems.Clear();
            }
        }
    }
}
